package com.itheima.service.impl;

<<<<<<< HEAD
import com.itheima.service.OrdersService;

public class OrdersServiceImpl implements OrdersService {
=======
import com.github.pagehelper.PageHelper;
import com.itheima.domain.Orders;
import com.itheima.domain.Traveller;
import com.itheima.mapper.OrdersMapper;
import com.itheima.service.OrdersService;
import com.itheima.util.MapperUtil;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    //查询所有订单信息
    @Override
    public List<Orders> findAll(String pageNum,String pageSize) throws Exception {
        //定义SqlSession对象
        SqlSession session = null;

        try {
            //获取session对象
            session = MapperUtil.getSession();

            //获取代理对象，即mapper对象
            OrdersMapper mapper = session.getMapper(OrdersMapper.class);

            //对获取的pageNum和pageSize进行类型转换
            int pageN = 1;//默认当前页为第一页
            int pageS = 5;//默认每页显示5条数据
            if(pageNum != null){
                pageN = Integer.parseInt(pageNum);
            }
            if(pageSize != null){
                pageS = Integer.parseInt(pageSize);
            }

            //设置分页参数:当前页码，每页现实的条数
            PageHelper.startPage(pageN,pageS);

            //调用findAll方法
            List<Orders> list = mapper.findAll();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            for (Orders order : list) {
                order.setOrderTimeStr(sdf.format(order.getOrderTime()));
            }

            //返回参数
            return list;
        }catch (Exception e){
            throw new RuntimeException("查询错误！");
        }finally {
            //提交事务，释放资源
            MapperUtil.close(session);
        }
    }

    //批量删除订单信息
    @Override
    public boolean deleteByIds(String ids) {
        //定义session对象
        SqlSession session = null;
        //设立flag，初始值为false，即删除失败
        boolean flag = false;
        try{
            //获取session对象
            session = MapperUtil.getSession();

            //获取代理对象
            OrdersMapper mapper = session.getMapper(OrdersMapper.class);

            //处理ids
            String[] idss = ids.split(",");

            //调用批量删除订单信息方法
            mapper.deleteOfOrder_Traveller(idss);
            mapper.deleteOfOrder(idss);

            //标记删除成功
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //释放资源
            MapperUtil.close(session);
        }

        //返回状态码
        return flag;
    }

    //根据Id查询
    @Override
    public Orders findById(String id) {
        //获取session对象
        SqlSession session = MapperUtil.getSession();
        System.out.println(id);
        //获取代理对象
        OrdersMapper mapper = session.getMapper(OrdersMapper.class);

        //设置时间转换格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //调用Id查询方法==orders信息和product信息
        Orders orders = mapper.findById(id);

        //转换时间
        orders.setOrderTimeStr(sdf.format(orders.getOrderTime()));
        orders.getProduct().setDepartureTimeStr(sdf.format(orders.getProduct().getDepartureTime()));

        //调用Id查询方法==member信息和traveller信息
        List<Orders> orders1 = mapper.findByMid(id);
        for(int i = 0; i < orders1.size(); i++){
            if(i == 0){
                orders.setMember(orders1.get(0).getMember());
                orders.setTravellers(orders1.get(0).getTravellers());
            }else {
                List<Traveller> travellers = orders1.get(i).getTravellers();
                for (Traveller traveller : travellers) {
                    orders.getTravellers().add(traveller);
                }
            }
        }

//        orders.setMember(orders1.getMember());

//        orders.setTravellers(orders1.getTravellers());

        //判断证件号码类型
        List<Traveller> list = orders.getTravellers();
        for (Traveller traveller : list) {
            if(traveller.getCredentialsType() == 0){
                traveller.setCredentialsTypeStr("身份证");
            }else {
                traveller.setCredentialsTypeStr("其他证件");
            }
        }

        //释放资源
        MapperUtil.close(session);

        //返回结果
        return orders;
    }

    //模糊查询
    @Override
    public List<Orders> query(String name, String pageNum, String pageSize) {
        //定义SqlSession对象
        SqlSession session = null;

        try {
            //获取session对象
            session = MapperUtil.getSession();

            //获取代理对象，即mapper对象
            OrdersMapper mapper = session.getMapper(OrdersMapper.class);

            //对获取的pageNum和pageSize进行类型转换
            int pageN = 1;//默认当前页为第一页
            int pageS = 5;//默认每页显示5条数据
            if(pageNum != null){
                pageN = Integer.parseInt(pageNum);
            }
            if(pageSize != null){
                pageS = Integer.parseInt(pageSize);
            }

            //设置分页参数:当前页码，每页现实的条数
            PageHelper.startPage(pageN,pageS);

            //调用findAll方法
            List<Orders> list = mapper.query(name);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            for (Orders order : list) {
                order.setOrderTimeStr(sdf.format(order.getOrderTime()));
            }

            //返回参数
            return list;
        }catch (Exception e){
            throw new RuntimeException("查询错误！");
        }finally {
            //提交事务，释放资源
            MapperUtil.close(session);
        }
    }
>>>>>>> 87a0f68c2b3b7de3b37de1968a6eac4ab534cee5
}
